package com.hyg.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Author: huangyuguang
 * Date: 2022/5/5
 * Description: 日期时间转换
 */
public class DateUtils {

    public static String getNoPointTime(String time){
        if(time != null){
            int end = time.lastIndexOf(".");
            if(time.length() - end < 5){
                time = time.substring(0,end);
            }
        }
        return time;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String getCurrentDate(){
        return stampToDate(System.currentTimeMillis());
    }

    public static String stampToDate(String datetime){
        long timestamp = dateToStamp(datetime);
        return stampToDate(timestamp);
    }

    public static String stampToDate(long times){
        return DateUtils.stampToDate("yyyy-MM-dd HH:mm:ss",times);
    }

    public static String stampToDate(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String format,long times){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(times);
    }

    public static long dateToStamp(String time){
        return DateUtils.dateToStamp("yyyy-MM-dd HH:mm:ss",time);
    }

    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamp(String format, String time){
        try{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            Date date = simpleDateFormat.parse(time);
            return date.getTime();
        }catch (Exception e){
            return -1;
        }
    }

    public static String setTime(long time) {
        if (time < 60) {
            if (time < 10) {
                return "00:0" + time;
            } else {
                return "00:" + time;
            }
        } else if (time < 3600) {
            if (time < 600) {
                if (time % 60 < 10) {
                    return "00:0" + time / 60 + ":0" + time % 60;
                } else {
                    return "00:0" + time / 60 + ":" + time % 60;
                }
            } else {
                if (time % 60 < 10) {
                    return "00:" + time / 60 + ":0" + time % 60;
                } else {
                    return "00:" + time / 60 + ":" + time % 60;
                }
            }
        } else {
            //小于10小时
            if (time < 36000) {
                //0+time/3600:  时
                if (time % 3600 < 600) {
                    if (time % 60 < 10) {
                        return "0" + time / 3600 + ":0" + time / 60 + ":0" + time % 60;
                    } else {
                        return "0" + time / 3600 + ":0" + time / 60 + ":" + time % 60;
                    }
                } else {
                    if (time % 60 < 10) {
                        return "0" + time / 3600 + ":" + time / 60 + ":0" + time % 60;
                    } else {
                        return "0" + time / 3600 + ":" + time / 60 + ":" + time % 60;
                    }
                }
            } else {
                if (time % 3600 < 600) {
                    if (time % 60 < 10) {
                        return time / 3600 + ":0" + time / 60 + ":0" + time % 60;
                    } else {
                        return time / 3600 + ":0" + time / 60 + ":" + time % 60;
                    }
                } else {
                    if (time % 60 < 10) {
                        return time / 3600 + ":" + time / 60 + ":0" + time % 60;
                    } else {
                        return time / 3600 + ":" + time / 60 + ":" + time % 60;
                    }
                }
            }
        }
    }

    public static String getWeek(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        String week = sdf.format(date);
        return week;
    }
}
